package com.example.clusterclientprovider;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>
 * </p>
 *
 * @author Zero
 * @since 2019/3/5
 */
@RestController
public class ExampleController {

    @Value("${hello:default}")
    private String hello;

    @Value("${spring.application.name}")
    private String appname;

    @Value("${server.port}")
    private Integer port;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(@RequestParam String name) {
        return "嗨，" + name + " " + hello;
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public Map<String, Object> test(@RequestParam String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("provider-name", appname);
        map.put("provider-port", port);
        map.put("provider-param", name);
        map.put("provider-session-id", getSession().getId());
        return map;
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attrs =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest();
    }

}
